/**
 * Created by Administrator on 2017/1/8.
 */

function ViewForm()
{
    operation= 'view';
    FormSubmit();
}
function SaveCloseForm()
{
    operation= 'saveclose';
    FormSubmit();
}

function UpdateForm()
{
    operation= 'update';
    FormSubmit();
}

function MoveForm()
{
    operation= 'move';
    FormSubmit();
}
//保留id
$(document).on('click', "#searchForm #reset", function(){
    clearForm('searchForm');
});

function clearForm(formId){
    //alert($("#"+formId).html());
    $("#"+formId+" input[type='text']").each(function(){
        // alert($(this).val());
        $(this).val("");
    });

    $("#"+formId+" input[type='checkbox']").each(function(){
        $(this).removeAttr("checked");
    });

    $("#"+formId+" select").each(function(){
        $(this).get(0).selectedIndex=0;
    });
}

function modalClose(target)
{
    $('#'+target).modal('hide');
}

function modalClear(target) {
    $('#'+target).find(".modal-body-view").empty();
}

function modalShow(target)
{
    $('#'+target).modal();
}

function modalHidden(target)
{
    //alert("hide:"+target);
    $('#'+target).modal('hide');
}

function modalLoad(target, url, title, post)
{
    if (!arguments[2]) title = '';
    if(url){
        $('#'+target).find(".modal-body-view").html('');
        var loadingDiv = '<p>页面加载中，请稍后...</p>';
        $('#'+target).find(".modal-body-view").html(loadingDiv); // 设置页面加载时的loading图片
        if (arguments[3] && post.length > 0) {
            $.post(url, post, function (res) {
                $('#' + target).find(".modal-body-view").html(res);
            })
        }else {
            $('#' + target).find(".modal-body-view").load(url);
        }
        $("#"+target).find(".modal-title").html(title);
        modalShow(target);
    }
}

function ReloadPageAfterUpdate(modalId, isClose, url)
{
    var treeNodeId = $('#jsTree_tree_selected_result').val();
    reloadForm(treeNodeId);
    if (isClose) {
        modalClear(modalId);
        modalHidden(modalId);
    }
    else
    {
        modalLoad(modalId,url);
    }
}
// 打开询问是否删除的模态框并设置需要删除的大修的ID
function showDeleteModal(key, url) {
    $("#deletePostId").val(key);// 将模态框中需要删除的大修的ID设为需要删除的ID
    $("#deletePostUrl").val(url);// 将模态框中需要删除的大修的ID设为需要删除的ID
    $("#delcfmOverhaul").modal({
        backdrop : 'static',
        keyboard : false
    });
}
/*请求删除接口*/
function deletePostInfo(){
    var key = $("#deletePostId").val();
    var url = $("#deletePostUrl").val();
    $.post(url, {key: key}, function (r) {
        if (r.result == 'success'){
            // location.reload();
            loadList();
            $("#deletePostId").val('');
            $("#deletePostUrl").val('');
            $("#delcfmOverhaul").modal('hide');
        }else{
            alert(r.message);
        }
    }, 'json');
    return false;
}
function deleteButton(key, url) {
    var treeNodeId = $('#jsTree_tree_selected_result').val();
    $.post(url, {key: key}, function (r) {
        if (r.result == 'success'){
            // location.reload();
            loadList(treeNodeId);
        }else{
            alert(r.message);
        }
    }, 'json');
    return false;
}

function batchDeleteBtn(url) {
    var datalist = $("input[name='selectedIds[]']:checked").map(function (e) {
        return $(this).val();
    }).get();
    var treeNodeId = $('#jsTree_tree_selected_result').val();
    $.post(url, {datalist: datalist}, function (r) {
        if (r.result == 'success'){
            loadList(treeNodeId);
            //location.reload();
        }else{
            alert(r.message);
        }
    }, 'json');
    return false;
}

function batchUpdateOrderBtn(url, formId) {
    var treeNodeId = $('#jsTree_tree_selected_result').val();
    $.ajax({
        url: url,
        data: $("#"+formId).serialize(),
        dataType: 'JSON',
        type: 'POST',
        success: function (r) {
            if (r.result == 'success') {
                loadList(treeNodeId);
                alert('更新成功');
                //location.reload();
            } else {
                alert(r.message);
            }
        },
        error: function (responseTxt) {
            //
        }
    });
    return false;
}

function statusButton(url) {
    var treeNodeId = $('#jsTree_tree_selected_result').val();
    $.post(url, function (r) {
        if (r.result == 'success'){
            //location.reload();
            loadList(treeNodeId);
        }else{
            alert(r.message);
        }
    }, 'json');
    return false;
}